import requests
import parsel
import json
from redis import StrictRedis
def main():
    url= 'https://movie.douban.com/chart'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
    r = requests.get(url,headers=headers)  # 请求网站
    body = r.content.decode('utf-8')
    sel = parsel.Selector(body)
    name = sel.css('.pl2 a::text').getall()
    number = sel.css('.rating_nums::text').getall()
    link = sel.css('.pl2 a::attr(href)').getall()
    data_list = []
    for i in range(0,10):
        data={}
        print(name[i])
        data['name'] = name[i].replace('/', '').strip()
        data['number'] = number[i]
        data['link'] = link[i]
        data_list.append(data)
    redis = StrictRedis(host='localhost', port=6379, decode_responses=True, password="tian0828")
    save_redis(redis, data_list)


def save_redis(redis, data_list):
    redis.delete('douban_hot')
    for o in data_list:
        # s = json.dumps(o.__dict__, ensure_ascii=False)
        s = json.dumps(o, ensure_ascii=False)
        # print(s)
        redis.rpush('douban_hot', s)
if __name__ == '__main__':
    main()